/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegistrarPagina.java
 *
 * Created on 13/11/2011, 05:31:56 PM
 */

package webcrawler;

import WebCrawlerBD.Pagina_TempBD;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author camposvc
 */
public class RegistrarPagina extends javax.swing.JFrame {

    private Pagina_TempBD pagina_tempBD= new Pagina_TempBD();
    private int prioridad;
    /** Creates new form RegistrarPagina */
    public RegistrarPagina() {
       initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbl_url = new javax.swing.JLabel();
        lbl_tipo_contenido = new javax.swing.JLabel();
        txt_url = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        combo_txtPrioridad = new javax.swing.JComboBox();
        txt_error = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(webcrawler.WebCrawlerApp.class).getContext().getResourceMap(RegistrarPagina.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setAutoscrolls(true);
        jPanel1.setName("jPanel1"); // NOI18N

        lbl_url.setText(resourceMap.getString("lbl_url.text")); // NOI18N
        lbl_url.setName("lbl_url"); // NOI18N

        lbl_tipo_contenido.setText(resourceMap.getString("lbl_tipo_contenido.text")); // NOI18N
        lbl_tipo_contenido.setName("lbl_tipo_contenido"); // NOI18N

        txt_url.setText(resourceMap.getString("txt_url.text")); // NOI18N
        txt_url.setName("txt_url"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        combo_txtPrioridad.setForeground(resourceMap.getColor("jComboRegistrar.foreground")); // NOI18N
        combo_txtPrioridad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Máxima Prioridad", "Prioridad Media", "Mínima Prioridad" }));
        combo_txtPrioridad.setName("jComboRegistrar"); // NOI18N

        txt_error.setForeground(resourceMap.getColor("txt_error.foreground")); // NOI18N
        txt_error.setText(resourceMap.getString("txt_error.text")); // NOI18N
        txt_error.setName("txt_error"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_tipo_contenido)
                            .addComponent(lbl_url))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_url)
                            .addComponent(combo_txtPrioridad, 0, 193, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addComponent(txt_error, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(jLabel4)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addComponent(txt_error))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_url)
                    .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_tipo_contenido)
                    .addComponent(combo_txtPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        txt_error.getAccessibleContext().setAccessibleName(resourceMap.getString("txt_error.AccessibleContext.accessibleName")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         this.prioridad=this.combo_txtPrioridad.getSelectedIndex()+1;

         if (this.txt_url.getText().equals("http://"))
         {
             this.txt_error.setForeground(Color.red);
             this.txt_error.setText("Escriba una dirección Válida");
         }
        else
        {
            try {
                URL url = new URL(this.txt_url.getText());
                } catch (Exception ex) {
                this.txt_error.setForeground(Color.red);
                this.txt_error.setText("URL inválida");
                return;
                }

            try
            {
                int contador=this.pagina_tempBD.PaginaIgual(this.txt_url.getText());
                    if (contador==0)
                    {
                        pagina_tempBD.RegistrarLink(this.txt_url.getText(), this.prioridad);
                        this.txt_error.setForeground(Color.BLUE);
                        this.txt_error.setText("La Página se registro correctamente");

                    }
                    else
                    {
                        this.txt_error.setForeground(Color.red);
                        this.txt_error.setText("La Página ya esta registrada");
                    }


            }
             catch(Exception e)
            {
                this.txt_error.setForeground(Color.red);
                this.txt_error.setText("Se produjo un Error "+e.toString());
            }


        }


}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RegistrarPagina().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox combo_txtPrioridad;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbl_tipo_contenido;
    private javax.swing.JLabel lbl_url;
    private javax.swing.JLabel txt_error;
    private javax.swing.JTextField txt_url;
    // End of variables declaration//GEN-END:variables

}
